home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / util / cdity / ModeProSrc.lha / Prefs / MPPSelectList.c < prev    next >
C/C++ Source or Header  |  1997-05-26  |  16KB  |  547 lines

  1. #include <debug.h>
  2. #include <intuition/intuition.h>
  3. #include <graphics/text.h>
  4. #include "mpp.h"
  5. #include <intuition/intuitionbase.h>
  6. #include <exec/execbase.h>
  7. #include <clib/extras_protos.h>
  8. #include <stdio.h>
  9. #include <math.h>
  10. #include <dos/dosextens.h>
  11. #include <extras/layoutgt.h>
  12. #include "mppexterns.h"
  13.  
  14. #define LGD_LIST 0
  15. #define LGD_OK      1
  16. #define LGD_CANCEL  2
  17.  
  18. struct ListRequester
  19. {
  20.   struct Window     *lr_Window;
  21.   APTR               lr_VisualInfo;
  22.   struct LG_Control *lr_GadControl;
  23.   struct List       *lr_List;
  24.   BOOL   lr_RetVal;
  25. };
  26.  
  27. void   CreateDefaultModeList(struct List *List);
  28. struct ModeNode *CreateModeNode(ULONG ID);
  29.  
  30. struct LG_Control *LayoutListGads(struct ListRequester *LReq);
  31.  
  32. //void FreeDefaultModeList(struct List *List);
  33.  
  34.  
  35. /*
  36. struct Gadget  *MakeListGadget  (struct List *List);
  37. void            RemListGadget    (void);
  38. */
  39.  
  40. BOOL SelectName(struct DefaultNode *DN)
  41. {
  42.   STRPTR name,namework;
  43.   struct List namelist;
  44.   struct Node *namenode;
  45.   LONG l,
  46.        ok=TRUE,
  47.        item;
  48.   BOOL rv=FALSE;
  49.   ULONG type;
  50.  
  51.   
  52.   if(DN)
  53.   {
  54.     NewList(&namelist);
  55.     if(DN->Type<4)
  56.     {
  57.       switch(DN->Type)
  58.       {
  59.         case PL_PROGRAMNAMES:
  60.           {
  61.             struct Task   *task;
  62.             struct List *tasklist;
  63.             struct Node *execnode;
  64.             UBYTE cliname[256];
  65.             
  66.             // Create a list of program names
  67.             Disable();
  68.             tasklist=&SysBase->TaskReady;
  69.             for(l=0;l<2;l++)
  70.             {
  71.               task=(struct Task *)tasklist[l].lh_Head;
  72.   
  73.               while(task->tc_Node.ln_Succ)
  74.               {
  75.                 execnode=(struct Node *)task;
  76.                 name=execnode->ln_Name;
  77.                 type=execnode->ln_Type;
  78.                 
  79.                 if(type==NT_PROCESS)
  80.                 {
  81.                   struct Process *proc;
  82.                   struct CommandLineInterface *cli;
  83.                   
  84.                   //kprintf("Process ");
  85.                   
  86.                   proc=(struct Process *)task;
  87.                   
  88.                   if(proc->pr_CLI)
  89.                   {
  90.                     cli=(struct CommandLineInterface *)BADDR(proc->pr_CLI);
  91.                     namework=(STRPTR)BADDR(cli->cli_CommandName);
  92.                     if(namework[0])
  93.                     {
  94.                       strncpy(cliname,&namework[1],namework[0]);
  95.                       cliname[namework[0]]=0;
  96.                       name=cliname;
  97.                     }
  98.                   }
  99.                 }
  100.                 //else
  101.                   //kprintf("Task    ");
  102.                   
  103.                 //kprintf("%s %ld\n",name,type);  
  104.                 
  105.                 if(name)
  106.                 {
  107.                   if(namenode=AllocNameNode(name))
  108.                   {
  109.                     namenode->ln_Type=type;
  110.                     namenode->ln_Pri=execnode->ln_Pri;
  111.                     EnqueueName(&namelist,namenode);
  112.                     
  113.                   }
  114.                   //else
  115.                   //  ok=FALSE;
  116.                 }
  117.                 task=(struct Task *)task->tc_Node.ln_Succ;
  118.               }
  119.             }
  120.             Enable();
  121.           }
  122.           break;
  123.         case PL_SCREENNAMES:          
  124.           {
  125.             struct Screen *scr;
  126.             ULONG  lock;
  127.             // Create a list of screens
  128.             lock=LockIBase(0);
  129.             scr=IntuitionBase->FirstScreen;
  130.             while(scr && ok)
  131.             {
  132.               if(scr->Title)
  133.               {
  134.                 if(namenode=AllocNameNode(scr->Title))
  135.                   EnqueueName(&namelist,namenode);
  136.                 else
  137.                   ok=FALSE;
  138.               }
  139.               scr=scr->NextScreen;
  140.             }
  141.             UnlockIBase(lock);
  142.           }
  143.           break;
  144.         case PL_PUBLICSCREENS:       
  145.           {   
  146.             struct Node *n;
  147.             struct List *list;
  148.             
  149.             // Create a list of public screens
  150.             list=LockPubScreenList();
  151.             n=list->lh_Head;
  152.             while(n->ln_Succ && ok)
  153.             {
  154.               if(n->ln_Name)
  155.               {
  156.                 if(namenode=AllocNameNode(n->ln_Name))
  157.                   EnqueueName(&namelist,namenode);
  158.                 else
  159.                   ok=FALSE;
  160.               }
  161.               n=n->ln_Succ;
  162.             }
  163.             UnlockPubScreenList();
  164.           }
  165.           break;
  166.         case PL_SCREENMODES:
  167.           CreateDefaultModeList(&namelist);
  168.           break;
  169.       }
  170.       
  171.       if((item=SelectReq(&namelist,GetString(MSG_SELWIN_PROGRAMS + DN->Type))) >=0)
  172.       {
  173.         STRPTR dummy;
  174.         
  175.         namenode=namelist.lh_Head;
  176.         while(item)
  177.         {
  178.           namenode=namenode->ln_Succ;
  179.           item--;
  180.         }
  181.         
  182.         dummy=DN->Def_Node.ln_Name;  /* The old name will be freed with FreeNameNodes() */
  183.         DN->Def_Node.ln_Name=namenode->ln_Name;
  184.         namenode->ln_Name=dummy;
  185.         if(DN->Type==PL_SCREENMODES)
  186.         {
  187.           DN->OrigDisplayID=((struct ModeNode *)namenode)->mn_DisplayID;
  188.         }
  189.         rv=TRUE;
  190.       }
  191.       FreeNameNodes(&namelist);
  192.     }
  193.   }
  194.   return(rv);
  195. }
  196.  
  197. struct Node *AllocNameNode(STRPTR Name)
  198. {
  199.   struct Node *n;
  200.   LONG l;
  201.   
  202.   if(n=AllocVec(sizeof(struct Node),MEMF_PUBLIC|MEMF_NO_EXPUNGE|MEMF_CLEAR))
  203.   {
  204.     n->ln_Pri=0;
  205.     l=strlen(Name);
  206.     if(n->ln_Name=AllocVec(l+1,MEMF_PUBLIC|MEMF_NO_EXPUNGE))
  207.     {
  208.       strncpy(n->ln_Name,Name,l);
  209.       n->ln_Name[l]=0;
  210.       return(n);
  211.     }
  212.     FreeVec(n);
  213.   }
  214.   return(0);
  215. }
  216.  
  217. void FreeNameNodes(struct List *List)
  218. {
  219.   struct Node *n;
  220.   
  221.   while(n=RemHead(List))
  222.   {
  223.     FreeVec(n->ln_Name);
  224.     FreeVec(n);
  225.   }
  226. }
  227.  
  228. void CreateDefaultModeList(struct List *List)
  229. {
  230.   struct DisplayInfo di;
  231.   struct ModeNode    *mn;
  232.   ULONG previd=MONITOR_ID_MASK,id;
  233.   
  234.   id=NextDisplayInfo(INVALID_ID);
  235.  
  236.   while(id!=INVALID_ID)
  237.   {
  238.     if(previd != (id & MONITOR_ID_MASK))
  239.     { // create an entry for all screen modes.
  240.       previd=(id & MONITOR_ID_MASK);
  241.       if(mn=CreateModeNode(id | ALL_MODES))
  242.         EnqueueName(List,(struct Node *)mn);      
  243.     } 
  244.     
  245.     if(GetDisplayInfoData(0,(UBYTE *)&di,sizeof(di),DTAG_DISP,id))
  246.     //if(!(di.PropertyFlags & SPECIAL_FLAGS)) // no HAM, EHB, 2PF etc.
  247.     if(!(di.PropertyFlags & DIPF_IS_PF2PRI)) // no PF2 etc.
  248.     {
  249.       if(mn=CreateModeNode(id))
  250.       {
  251.         EnqueueName(List,(struct Node *)mn);
  252.       }
  253.     }
  254.     id=NextDisplayInfo(id);
  255.   }
  256. }
  257.  
  258. struct ModeNode *CreateModeNode(ULONG ID)
  259. {
  260.   struct ModeNode *mn;
  261.   
  262.   if(mn=AllocVec(sizeof(struct ModeNode),MEMF_PUBLIC|MEMF_CLEAR))
  263.   {
  264.     if(mn->mn_Node.ln_Name=GetModeName(ID))
  265.     {
  266.       mn->mn_DisplayID=ID;
  267.       return(mn);
  268.     }
  269.     FreeVec(mn);
  270.   }
  271.   return(0);
  272. }
  273.  
  274. /********************************************************/
  275. /********************************************************/
  276.  
  277. WORD ListWinLeft,ListWinTop,ListWinWidth,ListWinHeight;
  278. BOOL ListFirstTime=TRUE;
  279. //APTR ListVI;
  280. //ULONG ListGadCount;
  281. //struct Window *ListWin;
  282. //struct Gadget *ListContext,*ListLV;
  283.  
  284. /* Opens the list requester */
  285. LONG SelectReq(struct List *List, STRPTR WinTitle)
  286. {
  287.   LONG nodes;
  288.   struct Requester *req;
  289.   LONG rv=-1;
  290.   struct IntuiMessage *imsg;
  291.   BOOL ok=TRUE;
  292.   WORD x1,y1,x2,y2;
  293.   LONG PrevSeconds =0,
  294.        PrevMicros  =0,
  295.        PrevCode    =-1;
  296.   struct ListRequester listreq;
  297.   
  298.   if(ListFirstTime)
  299.   {
  300.     ListWinWidth=   Win->Width/2;
  301.     ListWinHeight=  Win->Height-16;
  302.     ListWinLeft=    Win->LeftEdge+Win->Width/2-ListWinWidth/2;
  303.     ListWinTop=     Win->TopEdge+8;
  304.     ListWinWidth=   max(ListWinWidth,50);
  305.     ListWinHeight=  max(ListWinHeight,50);
  306.     ListFirstTime=FALSE;
  307.   }
  308.   
  309.   req=Busy(Win);
  310.   if(listreq.lr_Window=OpenWindowTags(0,
  311.                               WA_Left,          ListWinLeft,
  312.                               WA_Top,           ListWinTop,
  313.                               WA_Width,         ListWinWidth,
  314.                               WA_Height,        ListWinHeight,
  315.                               WA_MinWidth,      50,
  316.                               WA_MinHeight,     50,
  317.                               WA_MaxWidth,      -1,
  318.                               WA_MaxHeight,     -1,
  319.                               WA_Title,         WinTitle,
  320.                               WA_DragBar,       TRUE,
  321.                               WA_SizeGadget,    TRUE,
  322.                               WA_SizeBBottom,   TRUE,
  323.                               WA_DepthGadget,   TRUE,
  324.                               WA_SimpleRefresh, TRUE    ,
  325.                               WA_CloseGadget,   TRUE,
  326.                               WA_CustomScreen,  Scr,
  327.                               WA_Activate,      TRUE,
  328.                               WA_IDCMP,         IDCMP_REFRESHWINDOW |
  329.                                                 IDCMP_CLOSEWINDOW   |
  330.                                                 IDCMP_NEWSIZE       |
  331.                                                 IDCMP_VANILLAKEY    |
  332.                                                 IDCMP_RAWKEY        |
  333.                                                 LISTVIEWIDCMP,
  334.                               TAG_DONE))
  335.   {
  336.     x1=listreq.lr_Window->BorderLeft;
  337.     y1=listreq.lr_Window->BorderTop;
  338.     listreq.lr_List=List;
  339.     if(listreq.lr_VisualInfo=GetVisualInfo(listreq.lr_Window->WScreen,0))
  340.     {
  341.       if(LayoutListGads(&listreq))
  342.       {
  343.         LG_AddGadgets(listreq.lr_Window,listreq.lr_GadControl);
  344.         nodes=CountNodes(listreq.lr_List);
  345.         while(ok)
  346.         {
  347.           WaitPort(listreq.lr_Window->UserPort);
  348.           while(imsg=GT_GetIMsg(listreq.lr_Window->UserPort))
  349.           {
  350.             switch(imsg->Class)
  351.             {
  352.               case IDCMP_REFRESHWINDOW:
  353.                 GT_BeginRefresh(listreq.lr_Window);
  354.                 GT_EndRefresh(listreq.lr_Window,TRUE);
  355.                 break;
  356.               case IDCMP_CLOSEWINDOW:
  357.                 ok=FALSE;
  358.                 rv=-1;
  359.                 break;
  360.               case IDCMP_GADGETUP:
  361.                 if(DoubleClick(PrevSeconds,PrevMicros,imsg->Seconds,imsg->Micros) 
  362.                    && PrevCode==imsg->Code)
  363.                 {
  364.                   ok=FALSE;
  365.                   rv=imsg->Code;
  366.                 }
  367.                 else
  368.                 {
  369.                   PrevSeconds=imsg->Seconds;
  370.                   PrevMicros =imsg->Micros;
  371.                   PrevCode   =imsg->Code;
  372.                 }
  373.                 break;
  374.               case IDCMP_NEWSIZE:
  375.                 x2=listreq.lr_Window->Width -listreq.lr_Window->BorderRight-1;
  376.                 y2=listreq.lr_Window->Height-listreq.lr_Window->BorderBottom-1;
  377.                 
  378.                 LG_FreeGadgets(listreq.lr_GadControl);
  379.                 if(x2>x1 && y2>y1)
  380.                   EraseRect(listreq.lr_Window->RPort,x1,y1,x2,y2);
  381.                 RefreshWindowFrame(listreq.lr_Window);
  382.                 if(!LayoutListGads(&listreq))
  383.                 { 
  384.                   ok=FALSE;
  385.                   rv=-1;
  386.                 }
  387.                 else
  388.                 {
  389.                   LG_AddGadgets(listreq.lr_Window,listreq.lr_GadControl);
  390.                   ListWinWidth=   listreq.lr_Window->Width;
  391.                   ListWinHeight=  listreq.lr_Window->Height;
  392.                   ListWinLeft=    listreq.lr_Window->LeftEdge;
  393.                   ListWinTop=     listreq.lr_Window->TopEdge;
  394.                 }
  395.                 break;
  396.               case IDCMP_VANILLAKEY:
  397.                 switch(imsg->Code)
  398.                 {
  399.                   case '':
  400.                     ok=FALSE;
  401.                     rv=-1;
  402.                     break;
  403.                   case 13:
  404.                   case ' ':
  405.                     ok=FALSE;
  406.                     rv=PrevCode;
  407.                     break;
  408.                   
  409.                 }
  410.                 break; 
  411.               case IDCMP_RAWKEY:
  412.                 switch(imsg->Code)
  413.                 {
  414.                   case CURSORUP:
  415.                     PrevCode--;
  416.                     if(PrevCode<0)
  417.                       PrevCode=nodes-1;
  418.                     break;
  419.                   case CURSORDOWN:
  420.                     PrevCode++;
  421.                     if(PrevCode>=nodes)
  422.                       PrevCode=0;
  423.                     break;
  424.                 }
  425.                 LG_SetGadgetAttrs(listreq.lr_GadControl,LGD_LIST,
  426.                                                     GTLV_Selected       ,PrevCode,
  427.                                                     GTLV_MakeVisible    ,PrevCode,
  428.                                                     TAG_SKIP            ,( V39 ? 1:0),
  429.                                                     GTLV_Top            ,PrevCode,
  430.                                                     TAG_DONE);
  431.                 break;
  432.             }
  433.             GT_ReplyIMsg(imsg);
  434.           }
  435.         }
  436.         LG_FreeGadgets(listreq.lr_GadControl);
  437.       }
  438.       FreeVisualInfo(listreq.lr_VisualInfo);
  439.     }
  440.     CloseWindow(listreq.lr_Window);
  441.   }
  442.   NotBusy(Win,req);
  443.   return(rv);
  444. }
  445.  
  446.  
  447.  
  448.  
  449. struct LG_Control *LayoutListGads(struct ListRequester *LReq)
  450. {
  451.   WORD width,height;
  452.   struct LG_Control *lr_control=0;
  453.   //STRPTR ok,cancel;
  454.  
  455.   if(LReq)
  456.   {
  457.     if(LReq->lr_VisualInfo && LReq->lr_Window)
  458.     {
  459. //      rp=LReq->lr_Window->RPort; 
  460.  
  461.       width =LReq->lr_Window->Width  - LReq->lr_Window->BorderLeft- LReq->lr_Window->BorderRight  - 8;
  462.       height=LReq->lr_Window->Height - LReq->lr_Window->BorderTop - LReq->lr_Window->BorderBottom - 8;
  463.  
  464.       lr_control=LG_CreateGadgets( LG_VisualInfo        ,LReq->lr_VisualInfo,
  465.                                    LG_UseWindowOffsets  ,LReq->lr_Window,
  466.                                    LG_EraseRemoved      ,FALSE,
  467.                                    LG_OffsetX           ,4,
  468.                                    LG_OffsetY           ,4,
  469.                                    LG_TextAttr          ,TAttr,
  470.                                    
  471.                                    /* List Gadget */
  472.                                    LG_GadgetKind        ,LISTVIEW_KIND,
  473.                                    LG_GadgetID          ,LGD_LIST,
  474.                                    LG_Width             ,width,
  475.                                    LG_Height            ,height,
  476.                                    LG_GadgetTags        ,2,
  477.                                    GTLV_Labels          ,LReq->lr_List,
  478.                                    GTLV_ShowSelected    ,0,
  479.                                    LG_CreateGadget      ,0,
  480.                                  
  481.                                    TAG_DONE);
  482.     }
  483.   }
  484.   LReq->lr_GadControl=lr_control;
  485.   return(lr_control);
  486. }                                 
  487.  
  488.  
  489.  
  490. /*
  491.  
  492. struct Gadget *MakeListGadget(struct List *List)
  493. {
  494.   struct Gadget *gad;
  495.   struct NewGadget listng={0};
  496.  
  497.   listng.ng_VisualInfo=ListVI;
  498.   listng.ng_TextAttr  =TAttr;
  499.   listng.ng_LeftEdge  =listreq.lr_Window->BorderLeft  +4;
  500.   listng.ng_TopEdge   =listreq.lr_Window->BorderTop   +4;
  501.   listng.ng_Width     =listreq.lr_Window->Width-listreq.lr_Window->BorderLeft-listreq.lr_Window->BorderRight-8;
  502.   listng.ng_Height    =listreq.lr_Window->Height-listreq.lr_Window->BorderTop-listreq.lr_Window->BorderBottom-8;
  503.   
  504.   gad=CreateContext(&ListContext);
  505.  
  506.   ListLV=gad=CreateGadget(LISTVIEW_KIND, gad, &listng,
  507.                           GTLV_Labels           ,List,
  508.                           GTLV_ShowSelected     ,0,
  509.                           TAG_DONE);
  510.   if(!gad)
  511.   {
  512.     FreeGadgets(ListContext);
  513.     ListContext=0;
  514.     return(0);
  515.   }
  516.   else
  517.   {
  518.     struct Gadget *cgad;
  519.     
  520.     ListGadCount=0;
  521.     cgad=ListContext;
  522.     
  523.     while(cgad)
  524.     {
  525.       ListGadCount++;
  526.       cgad=cgad->NextGadget;
  527.     }
  528.     
  529.     AddGList(listreq.lr_Window,ListContext,-1,-1,0);
  530.     RefreshGList(ListContext,listreq.lr_Window,0,ListGadCount);
  531.     GT_RefreshWindow(listreq.lr_Window,NULL);
  532.   }
  533.   return(gad);
  534. }
  535.  
  536. void RemListGadget(void)
  537. {
  538.   if(ListContext)
  539.   {
  540.     RemoveGList(listreq.lr_Window,ListContext,ListGadCount);
  541.     FreeGadgets(ListContext);
  542.     ListContext=0;
  543.   }
  544. }
  545.  
  546. */
  547.